package TrackController;

import java.util.ArrayList;

import TrackModel.*;
import Utilities.Message;
import javafx.scene.*;
import javafx.scene.canvas.Canvas;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.TextField;
import javafx.scene.control.Label;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.application.*;
import javafx.fxml.FXMLLoader;
import javafx.concurrent.Task;

@SuppressWarnings({ "rawtypes", "unused" })
public class TrackControllerDriver extends Application {

	public static Parent root, child;
	public static Scene scene, cscene;
	public static Stage stage, cstage;
	public static Canvas canvas;
	public static ContextMenu contextMenu;
	public static Task task;

	public String[] data1 = {"1","1","100","0.5","55","0.5","285:323","24:43","0","0","13","0"};
	public String[] data2 = {"2","1","100","1.0","55","1.5","323:331","43:61","0","0","1","Pioneer","2"};
	public String[] data3 = {"3","1","100","1.5","55","3.0","331:342","61:79","0","1","2","0"};
	public String[] data4 = {"4","2","100","2.0","55","5.0","342:364","79:103","0","0","3","0"};
	public String[] data5 = {"5","2","100","3.0","55","8.0","364:387","103:113","0","0","4","0"};
	public String[] data6 = {"6","2","100","4.0","55","12.0","387:417","113:117","0","0","5","0"};
	public String[] data7 = {"7","3","100","5.0","55","17.0","417:471","117:108","0","0","6","0"};
	public String[] data8 = {"8","3","100","0.0","55","17.0","471:473","108:62","0","0","7","0"};
	public String[] data9 = {"9","3","100","-5.0","55","12.0","473:433","62:43","0","0","8","Edgebrook","0"};
	public String[] data10 = {"10","3","100","-5.0","55","7.0","433:377","43:31","0","1","9","3","1","1,19"};
	public String[] data11 = {"11","3","100","-4.0","55","3.0","377:333","31:27","0","0","10","0"};
	public String[] data12 = {"12","3","100","-3.0","55","0.0","333:281","27:25","0","0","11","2"};
	public String[] data13 = {"13","4","150","0.0","70","0.0","281:256","25:25","0","0","12:14","0"};
	public String[] data14 = {"14","4","150","0.0","70","0.0","256:236","25:25","0","0","13:15","0"};
	public String[] data15 = {"15","4","150","0.0","70","0.0","236:217","25:25","0","1","14:16","0"};
	public String[] data16 = {"16","4","150","0.0","70","0.0","217:194","25:25","0","0","15:17","Depths of Hell","1:3"};
	public String[] data17 = {"17","5","150","0.0","60","0.0","194:176","25:45","0","1","16:18","0"};
	public String[] data18 = {"18","5","150","0.0","60","0.0","176:158","45:65","0","0","17:19","0"};
	public String[] data19 = {"19","5","150","0.0","60","0.0","158:140","65:85","0","0","18:20","1"};
	public String[] data20 = {"20","5","150","0.0","60","0.0","140:121","85:104","0","0","19:21","0"};

	public Block block1 = new Block(data1);
	public Block block2 = new Block(data2);
	public Block block3 = new Block(data3);
	public Block block4 = new Block(data4);
	public Block block5 = new Block(data5);
	public Block block6 = new Block(data6);
	public Block block7 = new Block(data7);
	public Block block8 = new Block(data8);
	public Block block9 = new Block(data9);
	public Block block10 = new Block(data10);
	public Block block11 = new Block(data11);
	public Block block12 = new Block(data12);
	public Block block13 = new Block(data13);
	public Block block14 = new Block(data14);
	public Block block15 = new Block(data15);
	public Block block16 = new Block(data16);
	public Block block17 = new Block(data17);
	public Block block18 = new Block(data18);
	public Block block19 = new Block(data19);
	public Block block20 = new Block(data20);

	public ArrayList<Block> theBlocks = new ArrayList<Block>();

	public static TrackController simTC;


	public static void main(String[] args)
	{
		launch(args);
	}

	public void start(Stage st) throws Exception {

		root = FXMLLoader.load(getClass().getResource("TrackController_UI.fxml"));
		scene = new Scene(root, 810, 910);
		stage = st;
		stage.setTitle("Track Controller");
		stage.initStyle(StageStyle.UTILITY);
		stage.setScene(scene);
		stage.show(); 

		theBlocks.add(block1);
		theBlocks.add(block2);
		theBlocks.add(block3);
		theBlocks.add(block4);
		theBlocks.add(block5);
		theBlocks.add(block6);
		theBlocks.add(block7);
		theBlocks.add(block8);
		theBlocks.add(block9);
		theBlocks.add(block10);
		theBlocks.add(block11);
		theBlocks.add(block12);
		theBlocks.add(block13);
		theBlocks.add(block14);
		theBlocks.add(block15);
		theBlocks.add(block16);
		theBlocks.add(block17);
		theBlocks.add(block18);
		theBlocks.add(block19);
		theBlocks.add(block20);

		simTC = new TrackController(2, theBlocks);

		setUp();

	}

	public static void setUp()
	{
		task = new Task<Void>()
				{ 
			public Void call()
			{
				while(true)
				{
					if (isCancelled())
					{
						break;
					}

					tick();

					Platform.runLater( new Runnable()
					{
						public void run()
						{
							updateGUI();
						}
					});

				}

				return null;
			}
				};
				new Thread(task).start();
	}
	
	public static void updateGUI()
	{
		updateController();
		updateSwitchState();
		updateCrossingState();
		updateCrossingGateState();
		updateCrossingLightState();
		updateTrackHealth();
		updateTrainPresence();
	}
	
	public static void updateController()
	{
		Label laID = ((Label)root.lookup("#ControllerLabel"));
		int currControllerID = simTC.getTrackControllerID();
		String sID = Integer.toString(currControllerID);
		laID.setText(sID);
	}
	
	public static void updateSwitchState()
	{
		Label laSwi = ((Label)root.lookup("#SwitchStateLabel"));
		String currSwitchState = simTC.getSwitchState();
		laSwi.setText(currSwitchState);
	}
	
	public static void updateCrossingState()
	{
		Label laCro = ((Label)root.lookup("#CrossingStateLabel"));
		String currCrossingState = simTC.getCrossingState();
		laCro.setText(currCrossingState);
	}
	
	public static void updateCrossingGateState()
	{
		Label laGat = ((Label)root.lookup("#CrossingGateLabel"));
		String currCrossingGateState = simTC.getCrossingGateState();
		laGat.setText(currCrossingGateState);
	}
	
	public static void updateCrossingLightState()
	{
		Label laLig = ((Label)root.lookup("#CrossingLightLabel"));
		String currCrossingLightState = simTC.getCrossingLightState();
		laLig.setText(currCrossingLightState);
	}
	
	public static void updateTrackHealth()
	{
		Label laHea = ((Label)root.lookup("#TrackHealthLabel"));
		String currTrackHealth = simTC.getTrackHealth();
		laHea.setText(currTrackHealth);
	}
	
	public static void updateTrainPresence()
	{
		Label laPre = ((Label)root.lookup("#TrainPresenceLabel"));
		String currTrainPresence = simTC.getTrainPresence();
		laPre.setText(currTrainPresence);
	}

	public static void tick()
	{		
		simTC.tick();
	}
}
